#include <iostream>
#include <string.h>
#include <vector>
using namespace std;
class Person{
public:
    Person()
    :_name(new char[1]())
    {}
    Person(const char *name ,int age)
    :_name(new char[strlen(name)+1]())
    ,_age(age)
    {
        strcpy(_name ,name);
    }
    ~Person()
    {
        if(_name){
            delete  [] _name;
            _name = nullptr;
        }    
    }
    Person(const Person & rhs)
    :_name(new char[strlen(rhs._name)+1]())
    ,_age(rhs._age)
    {
        strcpy(_name,rhs._name);
    }
    Person& operator=(const Person & rhs){
        if(this != &rhs){
        delete  [] _name;
        _name = new char[strlen(rhs._name)+1]();
        strcpy(_name,rhs._name);
        _age = rhs._age;
        } 
        return *this;
    }
    virtual void display(){
        cout<<"姓名："<<_name<<endl;
        cout<<"年龄："<<_age<<endl;
    }
private:
    char * _name;
    int _age;
};
class Employee : public Person{
public:
    Employee()
    :_department(new char[1]() )
    {}
    Employee(const char*name,int age,const char *department,double price)
    :Person(name,age)
    ,_department(new char[strlen(department)+1]())
    ,_price(price)
    {
        strcpy(_department,department);
        totalPrice += _price;
        employeeCount++;
    }
    ~Employee(){
        if(_department){
            delete  []_department;
            _department = nullptr;
            totalPrice -= _price;
            employeeCount--;
        }
    }
    Employee(const Employee & rhs)
    :_department(new char[strlen(rhs._department)+1]())
    ,_price(rhs._price)
    ,Person(rhs)
    {
        strcpy(_department,rhs._department);
        totalPrice += _price;
        employeeCount++;
    }
    Employee & operator=(const Employee & rhs){
        if(this != & rhs){
            Person::operator=(rhs);
            delete [] _department;
            _department = new char[strlen(rhs._department)+1]();
            strcpy(_department,rhs._department);
            totalPrice -= _price;
            _price = rhs._price;
            totalPrice += _price;
        }
        return *this;
    }
    void display(){
        Person::display();
        cout<<"部门："<< _department<<endl;
        cout<<"工资："<< _price<<endl;
    }
    static double price_average(){
        return(double)totalPrice / employeeCount;
    }
private:
    char * _department;
    double _price;
    static double totalPrice;
    static int employeeCount;
};
double Employee::totalPrice = 0;
int Employee:: employeeCount = 0;
void test(){
 Employee e1("zs",30,"IT",5000);
 Employee e2("zh",24,"C++",8000);
 Employee e3 = e1;
 e1.display();
 e2.display();
 e3.display();
 cout<<"平均薪资："<<Employee::price_average()<<endl;
 
}
int main()
{
    test();
    return 0;
}

